/********************************************************************************
 *	Copyright 2011 Gerry Weirich
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at 
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *	$Id$
 ********************************************************************************/

package ch.rgw.cybert.persist;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.codehaus.jackson.annotate.JsonProperty;

import ch.rgw.cybert.Node;
import ch.rgw.cybert.Quant;

/**
 * decouple the Scala classes from the needs of the java wrapper ektorp
 * 
 * @author gerry
 * 
 */
public class NodeWrapper {
	private String label;
	private String revision;
	private int frequency;
	private String creator;
	
	public String getCreator() {
		return creator;
	}
	public void setCreator(String creator) {
		this.creator = creator;
	}

	
	private List<QuantWrapper> quants;

	public List<QuantWrapper> getQuants() {
		return quants;
	}
	public void setQuants(List<QuantWrapper> quants) {
		this.quants = quants;
	}
	public NodeWrapper(){}
	
	@SuppressWarnings("unchecked")
	public NodeWrapper(Node knol){
		label=knol.getLabel();
		revision=knol.getRevision();
		frequency=knol.getFrequency();
		quants=new ArrayList<QuantWrapper>();
		creator=knol.getCreator();
		List<Quant> orig=knol.listQuants();
		for(Quant q:orig){
			quants.add(new QuantWrapper(q));
		}
	}
	
	void merge(NodeWrapper other){
		frequency=Math.max(frequency, other.frequency);
		Iterator<QuantWrapper> it=other.quants.iterator();
		while(it.hasNext()){
			QuantWrapper qw=it.next();
			boolean bExist=false;
			for(QuantWrapper qMine:quants){
				if(qMine.getQuestionID()==qw.getQuestionID()){
					qMine.merge(qw);
					bExist=true;
					break;
				}
			}
			if(!bExist){
				quants.add(qw);
			}
		}
		
	}
	@JsonProperty("_id")
	public String getId() {
		return label;
	}

	@JsonProperty("_id")
	public void setId(String id) {
		label = id;
	}

	@JsonProperty("_rev")
	public String getRevision() {
		return revision;
	}

	public String getType() {
		return "Node";
	}
	
	public void setType(String type){
		// nothing
	}

	@JsonProperty("_rev")
	public void setRevision(String r) {
		revision=r;
	}

	public int getFrequency() {
		return frequency;
	}

	public void setFrequency(int freq) {
		frequency=freq;
	}

	
	public Node createNode(){
		Node knol=new Node(label,creator);
		knol.setFrequency(getFrequency());
		knol.setRevision(getRevision());
		for(QuantWrapper qw:getQuants()){
			knol.addQuant(qw.createQuant());
		}
		return knol;
	}
	
	
}
